<template>
  <div :class="className" :style="{height:height,width:width}">
  </div>
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    incomeArr:{
      type: Array,
      default: []
    },
    expendArr:{
      type: Array,
      default: []
    },
    title:{
      type: String,
      default: '2023年收入支出报表'
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '50%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    incomeArr: {
      deep: true,
      handler(val) {
        this.setOptions()
      }
    },
    title:{
      deep: true,
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    },
    setOptions() {
      this.chart.setOption({
        title:{
          show:true,
          text: this.title,
          x: "center",
          y: "bottom",
          padding: -2
        },
        xAxis: {
          data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月',],
          boundaryGap: true,
          axisTick: {
            show: true,
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          type: 'value',
          axisTick: {
            show: true
          }
        },
        legend: {
          data: ['支出', '收入'],

        },
        series: [{
          name: '支出', itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: this.expendArr,
          animationDuration: 2000,
          animationEasing: 'cubicInOut'
        },
        {
          name: '收入',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              },
            }
          },
          data: this.incomeArr,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }]
      })
    }
  }
}
</script>
